<?php
	if(isset($_REQUEST['xml']) && ($_REQUEST['xml'] == '1')){
		$xml=1;
	}else{$xml=0;}
	
	session_start();
	if(session_is_registered(username)){
		$name=$_SESSION['username'];
	}
	/*else{
		//header("refresh:3; url=../../home.html");
		//echo "You need to be logged in to access this page - redirecting in 3 seconds...";
		 if($xml==0){
			$error['Result'] = array('ListLinks'=>'notLoggedInError');
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			//childs of root
			$error = $doc->createElement('ListLinks');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('notLoggedInError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}*/
	
	try{
		$db = new PDO('sqlite:../../projecto1.db');
	}
	catch(PDOException $err){
		if($xml==0){
			$error['Result'] = array('ListLinks'=>'DatabaseError');
			echo json_encode($error);
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('ListLinks');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('DatabaseError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
	}
	
	$stmt = $db->prepare('SELECT * FROM Link ORDER BY idLink asc');	
	$stmt->execute();
	$link_list = $stmt->fetchAll();
	
	$contador=0;
	foreach ($link_list as $link) {
		$json['Result']['ListLinks']['Link'][$contador] = array('linkURL'=>$link['linkURL'],'hits'=>$link['hits']);
		$contador++;
	}
	if($xml==0){
		echo json_encode($json);
		die;
	}else{
		$doc = new DomDocument('1.0');
		$doc->formatOutput = TRUE;
		// create root node
		$root = $doc->createElement('Result');
		$root = $doc->appendChild($root);
		$attr = $doc->createAttribute("xmlns:xsi");
		$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
		$root->appendChild($attr);
		$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
		$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
		$root->appendChild($attr);
		$error = $doc->createElement('ListLinks');
		$error= $root->appendChild($error);
		//childs of root
		foreach($json as $j){
			foreach($j as $j1){
				foreach($j1 as $j2){
					foreach($j2 as $j3){
						$error1 = $doc->createElement('Link');
						$error1 = $error->appendChild($error1);			
						$error2 = $doc->createElement('LinkURL');
						$error2= $error1->appendChild($error2);
						$value = $doc->createTextNode($j3['linkURL']);
						$value = $error2->appendChild($value);
						
						$error2 = $doc->createElement('Hits');
						$error2 = $error1->appendChild($error2);
						$value = $doc->createTextNode($j3['hits']);
						$value = $error2->appendChild($value);
					}
				}
			}
		}
		echo $doc->saveXML();
		die;
	}	
?>